﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using weikebuluo.DAL;
using weikebuluo.Common;
using weikebuluo.Model;

namespace weikebuluo.web.wechat.dazhuanpan
{
    public partial class dazhuanpan : System.Web.UI.Page
    {
        h_dzp_dal dzpdal = new h_dzp_dal();
        protected h_dzp_model dzpmodel = new h_dzp_model();
        h_dzplog_dal dzplogdal = new h_dzplog_dal();
        h_dzplog_model dzplogmodel = new h_dzplog_model();
        Random rand = new Random();
        protected DataTable dzplog1 = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
           

            if (!string.IsNullOrEmpty(Request.QueryString["hid"]))
            {
                dzplog1 = dzplogdal.GetList("wxid='" + Request.QueryString["wxid"] + "' and dzp_id=" + Request.QueryString["hid"]).Tables[0];
                DataTable dtdzp = dzpdal.GetList("dzp_id=" + Request.QueryString["hid"]).Tables[0];
                dzpmodel = dzpdal.GetModel(int.Parse(Request.QueryString["hid"].ToString()));
                if (DateTime.Parse(dtdzp.Rows[0]["dzp_time1"].ToString()) > DateTime.Now)
                {
                    Response.Write("活动未开始！");
                    Response.End();
                }
                else if (DateTime.Parse(dtdzp.Rows[0]["dzp_time2"].ToString()) < DateTime.Now)
                {
                    Response.Write("活动结束！");
                    Response.End();
                }
            }

            

            switch (Request.QueryString["action"]) { 
                case "dzp":
                    dzp();
                    break;
                case "add":
                    add();
                    break;           
            }

        }

        private void dzp() {

            DataTable dzplog = dzplogdal.GetList("wxid='"+Request.QueryString["wxid"]+"' and  dzp_id="+Request.QueryString["hid"]).Tables[0];

            if (dzplog.Rows.Count > 0)
            {
                Response.Write("{\"re\":\"1\"}");

            }
            else {
                dzpmodel = dzpdal.GetModel(int.Parse(Request.QueryString["hid"]));
                int r = rand.Next(1, 999);//本次幸运数字
                int gl = 90;
                string txt = "未中奖！";
                if (r < dzpmodel.dzp_gl1)
                {
                    if (dzpmodel.dzp_num1 > dzplogdal.GetRecordCount("dzplog_award = '一等奖' and dzp_id=" + Request.QueryString["hid"]))
                    {
                        gl = 0;
                        dzplogmodel.dzplog_award = "一等奖";
                        txt = "一等奖";
                        dzplogmodel.dzplog_stutas = 1;
                    }
                    else {
                        gl = 90;
                        dzplogmodel.dzplog_award = "未中奖";
                        dzplogmodel.dzplog_stutas = 0;
                    }
                    
                }
                else if (dzpmodel.dzp_gl1 < r && r < (dzpmodel.dzp_gl1 + dzpmodel.dzp_gl2))
                {
                    if (dzpmodel.dzp_num2 > dzplogdal.GetRecordCount("dzplog_award = '二等奖' and dzp_id=" + Request.QueryString["hid"]))
                    {
                        gl = 225;
                        dzplogmodel.dzplog_award = "二等奖";
                        txt = "二等奖";
                        dzplogmodel.dzplog_stutas = 1;
                    }
                    else
                    {
                        gl = 90;
                        dzplogmodel.dzplog_award = "未中奖";
                        dzplogmodel.dzplog_stutas = 0;
                    }
                }
                else if (r > (dzpmodel.dzp_gl1 + dzpmodel.dzp_gl2) && r < (dzpmodel.dzp_gl1 + dzpmodel.dzp_gl2 + dzpmodel.dzp_hl3))
                {
                    if (dzpmodel.dzp_num3 > dzplogdal.GetRecordCount("dzplog_award = '三等奖' and dzp_id=" + Request.QueryString["hid"]))
                    {
                        gl = 135;
                        dzplogmodel.dzplog_award = "三等奖";
                        txt = "三等奖";
                        dzplogmodel.dzplog_stutas = 1;
                    }
                    else
                    {
                        gl = 90;
                        dzplogmodel.dzplog_award = "未中奖";
                        dzplogmodel.dzplog_stutas = 0;
                    }
                }
                else {
                    dzplogmodel.dzplog_award = "未中奖";
                    dzplogmodel.dzplog_stutas = 0;
                }
                
                dzplogmodel.dzp_id = int.Parse(Request.QueryString["hid"]);
                dzplogmodel.wxid = Request.QueryString["wxid"];
                
                dzplogdal.Add(dzplogmodel);
                Response.Write("{\"re\":\"0\",\"gl\":" + gl + ",\"txt\":\"" + txt + "\",\"flag\":\""+dzplogmodel.dzplog_stutas+"\"}");
            }
            Response.End();
        }

        private void add() {
            if (!string.IsNullOrEmpty(Request.Form["hid"]))
            {
                DataTable dtdzplog = dzplogdal.GetList("dzp_id=" + Request.Form["hid"] + " and wxid='" + Request.Form["wxid"] + "'").Tables[0];
                dzplogmodel = dzplogdal.GetModel(int.Parse(dtdzplog.Rows[0]["dzplog_id"].ToString()));
            }
            dzplogmodel.dzplog_name = Request.Form["uname"];
            dzplogmodel.dzplog_phone = Request.Form["umobile"];
            dzplogmodel.dzplog_stutas = 1;
            dzplogdal.Update(dzplogmodel);
            Response.Write(1);
            Response.End();
        }
    }
}